﻿using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.Persistent.Base;
using EasyXaf.EntityDesigner.BusinessObjects.Entities;
using EasyXaf.EntityDesigner.BusinessObjects.Entities.EntityViews;
using EasyXaf.EntityDesigner.Editors.DetailViewEditors;

namespace EasyXaf.EntityDesigner.Controllers;

public class EntityDetailViewController : ObjectViewController<ListView, Entity>
{
    public SimpleAction EditEntityDetailViewAction { get; }

    public EntityDetailViewController()
    {
        EditEntityDetailViewAction = new SimpleAction(this, $"{Name}-{nameof(EditEntityDetailViewAction)}", PredefinedCategory.Tools)
        {
            Caption = "详情视图",
            ImageName = "ModelEditor_DetailView",
            SelectionDependencyType = SelectionDependencyType.RequireSingleObject
        };
    }

    protected override void OnActivated()
    {
        base.OnActivated();

        EditEntityDetailViewAction.Execute += EditEntityDetailViewAction_Execute;
    }

    protected override void OnDeactivated()
    {
        EditEntityDetailViewAction.Execute -= EditEntityDetailViewAction_Execute;

        base.OnDeactivated();
    }

    private void EditEntityDetailView(EntityView entityView)
    {
        var objectSpace = Application.CreateObjectSpace(typeof(DetailViewObject));
        var detailViewObject = objectSpace.CreateObject<DetailViewObject>();
        detailViewObject.View = objectSpace.GetObject(entityView);
        var detailView = Application.CreateDetailView(objectSpace, detailViewObject);
        Application.ShowViewStrategy.ShowViewInPopupWindow(detailView, () =>
        {
            View.Refresh(true);
        });
    }

    private void EditEntityDetailViewAction_Execute(object sender, SimpleActionExecuteEventArgs e)
    {
        var entityViews = ViewCurrentObject.Views.Where(v => v.ViewType == EntityViewType.DetailView).ToList();
        if (entityViews.Count == 1)
        {
            EditEntityDetailView(entityViews[0]);
        }
        else if (entityViews.Count > 1)
        {
            var objectSpace = Application.CreateObjectSpace(typeof(DetailViewObject));
            var viewId = Application.GetListViewId(typeof(DetailViewObject));
            var collectionSource = Application.CreateCollectionSource(objectSpace, typeof(DetailViewObject), viewId);
            foreach (var entityView in entityViews)
            {
                collectionSource.Add(new DetailViewObject { View = objectSpace.GetObject(entityView) });
            }
            var listView = Application.CreateListView(viewId, collectionSource, true);
            Application.ShowViewStrategy.ShowViewInPopupWindow(listView, () =>
            {
                if (listView.SelectedObjects.Count > 0)
                {
                    EditEntityDetailView(((DetailViewObject)listView.SelectedObjects[0]).View);
                }
            });
        }
        else
        {
            var entityView = ObjectSpace.CreateObject<EntityView>();
            entityView.ViewType = EntityViewType.DetailView;
            entityView.Entity = ViewCurrentObject;
            ObjectSpace.CommitChanges();
            EditEntityDetailView(entityView);
        }
    }
}
